﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Diagnostics;
using System.ComponentModel;

namespace wpControl1
{
    /// <summary>
    /// Class that manages the list of images.
    /// </summary>
    class ImageManager
    {
        /// <summary>
        /// The list of images.
        /// </summary>
        public List<string> Images;

        

        /// <summary>
        /// Creates a new list of images.
        /// </summary>
        public void InitImageStack()
        {
            this.Images = new List<string>();
        }

        /// <summary>
        /// Adds one or more new images to the list. The images are selected by an OpenFileDialog.
        /// </summary>
        public void AddImage()
        {
            string filePath;

            // Create dialog
            OpenFileDialog AddImageDialog = new OpenFileDialog();
            AddImageDialog.Filter = "Bitmaps|*.bmp";
            AddImageDialog.Title = Program.LanguageManager.GetString("Caption_AddImage");
            AddImageDialog.Multiselect = true;

            // If file has been selected
            if (AddImageDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // Insert image(s) into the lists
                    foreach (string file in AddImageDialog.FileNames)
                    {
                        filePath = Path.GetFullPath(file);
                        // Use Add() for inserting at the end of the list
                        this.Images.Insert(0, filePath);
                        Program.ZoomStyleManager.Styles.Insert(0, Wallpaper.Style.Stretched);
                        Program.ControlPanel.listBoxImages.Items.Insert(0, filePath);
                    }
                }
                catch (SecurityException)
                {
                    MessageBox.Show(Program.LanguageManager.GetString("Error_AddImage_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Removes the image selected in the listbox.
        /// </summary>
        public void RemoveImage()
        {
            int deleteIndex;
            try
            {
                // Get index of selected image
                deleteIndex = Program.ControlPanel.listBoxImages.SelectedIndex;
                // Remove from lists
                this.Images.RemoveAt(deleteIndex);
                Program.ZoomStyleManager.Styles.RemoveAt(deleteIndex);
                // Remove from listbox
                Program.ControlPanel.listBoxImages.Items.Remove(Program.ControlPanel.listBoxImages.SelectedItem);
                // Select new image (item above)
                Program.ControlPanel.listBoxImages.SelectedIndex = deleteIndex - 1;
            }
            catch (ArgumentException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_RemoveImage_Argument"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Removes all images in the list.
        /// </summary>
        public void RemoveAllImages()
        {
            // Go through every item in the list
            int items = this.Images.Count;
            for (int i = 0; i < items; i++)
            {
                // Remove current image from lists
                this.Images.RemoveAt(0);
                Program.ZoomStyleManager.Styles.RemoveAt(0);
                // Remove from listbox
                Program.ControlPanel.listBoxImages.Items.RemoveAt(0);
            }
            // Keep interface up-to-date
            Program.ControlPanel.UpdateImageButtons();
        }

        /// <summary>
        /// Opens the image with the associated program as a preview.
        /// </summary>
        public void PreviewImage()
        {
            string filePath;
            try
            {
                // Get path of selected image
                filePath = Program.ControlPanel.listBoxImages.GetItemText(Program.ControlPanel.listBoxImages.SelectedItem);
                // Open image with associated program
                Process.Start(filePath);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_PreviewImage_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Win32Exception e)
            {
                MessageBox.Show(e.Message, Program.LanguageManager.GetString("Caption_WindowsError"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Moves the selected image up in the list.
        /// </summary>
        internal void MoveImageUp()
        {
            // Get index of selected image
            int index = Program.ControlPanel.listBoxImages.SelectedIndex;
            // Get listbox item
            object listboxItem = Program.ControlPanel.listBoxImages.SelectedItem;
            // Get image path
            string imageItem = Program.ImageManager.Images[index];
            // Get style info
            Wallpaper.Style styleItem = Program.ZoomStyleManager.Styles[index];

            // The first image can't be moved up
            if (index > 0)
            {
                // Remove image from listbox
                Program.ControlPanel.listBoxImages.Items.RemoveAt(index);
                // Remove image from the lists
                Program.ImageManager.Images.RemoveAt(index);
                Program.ZoomStyleManager.Styles.RemoveAt(index);
                // Insert old item into listbox at its new position
                Program.ControlPanel.listBoxImages.Items.Insert(index - 1, listboxItem);
                // Insert old item into lists at its new position
                Program.ImageManager.Images.Insert(index - 1, imageItem);
                Program.ZoomStyleManager.Styles.Insert(index - 1, styleItem);
                // Keep the item selected
                Program.ControlPanel.listBoxImages.SelectedIndex = index - 1;
            }
        }

        /// <summary>
        /// Moves the selected image down in the list.
        /// </summary>
        internal void MoveImageDown()
        {
            // Get index of selected image
            int index = Program.ControlPanel.listBoxImages.SelectedIndex;
            // Get listbox item
            object listboxItem = Program.ControlPanel.listBoxImages.SelectedItem;
            // Get image path
            string imageItem = Program.ImageManager.Images[index];
            // Get style info
            Wallpaper.Style styleItem = Program.ZoomStyleManager.Styles[index];

            // Last item can't be moved down
            if (index < Program.ControlPanel.listBoxImages.Items.Count - 1)
            {
                // Remove item from listbox
                Program.ControlPanel.listBoxImages.Items.RemoveAt(index);
                // Remove from lists
                Program.ImageManager.Images.RemoveAt(index);
                Program.ZoomStyleManager.Styles.RemoveAt(index);
                // Insert old item into listbox at new position
                Program.ControlPanel.listBoxImages.Items.Insert(index + 1, listboxItem);
                // Insert old item into lists at new position
                Program.ImageManager.Images.Insert(index + 1, imageItem);
                Program.ZoomStyleManager.Styles.Insert(index + 1, styleItem);
                // Keep item selected
                Program.ControlPanel.listBoxImages.SelectedIndex = index + 1;
            }
        }
    }
}
